import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile
import matplotlib.mlab as mlab
import scipy.stats as st
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def fivenum(v):
    """Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input vector, a list or array of numbers based on 1.5 times the interquartile distance"""
    import numpy as np
    from scipy.stats import scoreatpercentile
    try:
        np.sum(v)
    except TypeError:
        print('Error: you must provide a list or array of only numbers')
    q1 = scoreatpercentile(v,25)
    q3 = scoreatpercentile(v,75)
    md = np.median(v)
    return np.min(v), q1, md, q3, np.max(v),


def test():
    np.random.seed(7654567)
    rvs = st.norm.rvs(loc=5,scale=10,size=(50,2))
    print rvs
    print st.ttest_1samp(rvs,5.0)
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    #inputCSV = "C:/Documents and Settings/wang322/My Documents/My Dropbox/STAT515/proj/st_louis_mo_temp_prep.csv"
    inputCSV = "C:/_DATA/CancerData/NC/permutate.csv"
    dataMatrix = build_data_list(inputCSV) # [year, temp, prep]
    iLen = dataMatrix.shape

    temp_value = dataMatrix[np.argsort(-dataMatrix[:,3]),:]
    p = iLen[0]/10
    value = temp_value[:int(p),4]
    
    value = dataMatrix[:,5]
    #print value[:10]
    mu = np.mean(value)
    sigma = np.std(value)
    count, bins, ignored = plt.hist(value, 30, normed=True)
    y = mlab.normpdf(bins, mu, sigma)
    l = plt.plot(bins, y, 'r--', linewidth=1)
    '''
    #i = 0
    value_1 = dataMatrix[:,4]
    #value_1 = value_1/1000
    print value_1.shape
    #for item in value_1:
        #if item < 9025.725:
            #i += 1
    mu_1 = np.mean(value_1)
    sigma_1 = np.std(value_1)
    count, bins, ignored = plt.hist(value_1, 40, normed=1)
    y_1 = mlab.normpdf(bins, mu_1, sigma_1)
    l_1 = plt.plot(bins, y_1, 'r--', linewidth=1)
    '''
    #print value_1
    #print i
    #plt.axis([np.min(value_1), np.max(value_1), 0, 0.01])
    plt.show()
    
    print "end at " + getCurTime()
    print "========================================================================"  

           
